home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 1082 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.7 KB

  1. Path: news.bridge.net!news
  2. From: David Byrden <100101.2547@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How best to handle constructor failure?
  5. Date: 9 Jan 1996 05:42:52 GMT
  6. Organization: self-employed
  7. Message-ID: <4csv8s$9gg@news.bridge.net>
  8. References: <4c36gl$5e5@nnrp1.news.primenet.com> <4cimnp$hne@news1.panix.com>
  9. NNTP-Posting-Host: ppp-mia1-58.bridge.net
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.1N (Windows; I; 16bit)
  14.  
  15.  
  16. Jeffrey;
  17.  
  18. >> Yes, using auto pointers will avoid memory leaks from un-destructed
  19. >>  objects. The problem, however, is how to alter the sequence of code
  20. >>  existing OUTSIDE of the constructor, based on the success or failure
  21. >>  of the constructor.
  22.  
  23.  I don't understand. You want to "bypass the code that follows the
  24. constructor" presumably because it will try to use the object which 
  25. failed to construct. 
  26.  
  27.  But surely, if a ctor fails, then it will throw an exception, and the 
  28. exception will propagate out of it, and that will terminate the 
  29. SURROUNDING try block, including all the dangerous code?
  30.  
  31.  Yes, it is possible to put try and catch blocks within the ctor, and 
  32. thus to catch and 'swallow' an exception - but you should only swallow it 
  33. if you can SOLVE the associated problem and succesfully construct the 
  34. object after all.
  35.  
  36.  Does this answer your question?
  37.  
  38.  
  39. p.s. two-pase construction, where the ctor makes an invalid object and a 
  40. second function must be called, is no longer a useful C++ technique; both 
  41. for the reasons that you pointed out, AND because you cannot tell a 
  42. template that your particular class needs a special function to be 
  43. called.
  44.  
  45.  
  46.                     David
  47.  
  48.       ||  My opinions ARE those of my employer  ||
  49.  
  50.  
  51.  
  52.  
  53.